package com.example.emergencyattempt01.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.emergencyattempt01.R;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

public class HistoryTab_ListViewAdapter extends BaseAdapter {
    List<Map<String,Object>> itemList;
    Context context;
    ViewHolder viewHolder;



    class ViewHolder{
        TextView bidField;
        TextView lidField;
        TextView uidField;
        TextView userNameField;
        TextView identityField;
        TextView ownBooksField;
        TextView bookNameField;
        TextView ISBNField;
        TextView borrowTimeField;

        TextView returnTime;
    }

    public HistoryTab_ListViewAdapter(List<Map<String, Object>> itemList, Context context) {
        this.itemList = itemList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public Object getItem(int position) {
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return (long)itemList.get(position).get("bid");
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater=LayoutInflater.from(context);
        if (convertView==null)
        {
            convertView=inflater.inflate(R.layout.history_tab_list_item,null);
            viewHolder=new ViewHolder();
            viewHolder.bidField=convertView.findViewById(R.id.history_borrow_bid);
            viewHolder.uidField=convertView.findViewById(R.id.history_borrow_uid);
            viewHolder.lidField=convertView.findViewById(R.id.history_borrow_lid);
            viewHolder.bookNameField=convertView.findViewById(R.id.history_borrow_bookName);
            viewHolder.borrowTimeField=convertView.findViewById(R.id.history_borrow_borrowTime);
            viewHolder.userNameField=convertView.findViewById(R.id.history_borrow_userName);
            viewHolder.identityField=convertView.findViewById(R.id.history_borrow_identity);
            viewHolder.ownBooksField=convertView.findViewById(R.id.history_borrow_ownBooks);
            viewHolder.ISBNField=convertView.findViewById(R.id.history_borrow_ISBN);
            viewHolder.returnTime=convertView.findViewById(R.id.history_returnTime);

            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder=(ViewHolder) convertView.getTag();
        }
        // 获取各值
        long bid;
        long lid;
        long uid;
        String userName;
        String identity;
        String bookName;
        int ownBooks;
        String ISBN;
        long borrowTime;
        long lastTime;
        long returnTime;

        uid=(long)itemList.get(position).get("uid");
        bid=(long)itemList.get(position).get("bid");
        lid=(long)itemList.get(position).get("lid");
        borrowTime=(long)itemList.get(position).get("borrowTime");
        lastTime=(long)itemList.get(position).get("lastTime");

        userName=(String)itemList.get(position).get("userName");
        identity=(String)itemList.get(position).get("identity");
        bookName=(String)itemList.get(position).get("bookName");
        ownBooks=(int)itemList.get(position).get("ownBooks");
        ISBN=(String)itemList.get(position).get("ISBN");
        returnTime=(long)itemList.get(position).get("returnTime");


        // 对单纯的文字部分设值
        viewHolder.uidField.setText("uid:"+uid);
        viewHolder.bidField.setText("借阅号:"+bid);
        viewHolder.lidField.setText("馆藏号:"+lid);
        viewHolder.userNameField.setText(userName);
        viewHolder.identityField.setText(identity);
        viewHolder.bookNameField.setText(bookName);
        if(ownBooks<=5)
            viewHolder.ownBooksField.setText(ownBooks+"本");
        else
            viewHolder.ownBooksField.setText("5本");
        viewHolder.ISBNField.setText(ISBN);

        // 对时间进行一个处理
        String borrowTimeDisplay=millisToString(borrowTime);
        viewHolder.borrowTimeField.setText(borrowTimeDisplay);

        // 归还时间
        String returnTimeDisplay=millisToString(returnTime);
        viewHolder.returnTime.setText("归还于"+returnTimeDisplay);


        return convertView;
    }

    private String millisToString(long timeInMillis) {
        //新建一个calendar对象帮助转化出年月日时分秒
        Calendar calendar=Calendar.getInstance();
        calendar.setTimeInMillis(timeInMillis);
        /**
         * calendar中月份是从0起的
         */
        String year=unify( calendar.get(Calendar.YEAR));
        String month=unify( calendar.get(Calendar.MONTH)+1);
        String day=unify( calendar.get(Calendar.DATE));
        String hour=unify( calendar.get(Calendar.HOUR_OF_DAY));
        String minute=unify( calendar.get(Calendar.MINUTE));
        String second=unify( calendar.get(Calendar.SECOND));

        String target=year+"/"+month+"/"+day+"  "+hour+":"+minute+":"+second;
        return  target;
    }
    private String unify(int num)
    {
        String target=null;
        if(num<10)
        {
            target="0"+num;
        }
        else
            target=""+num;
        return target;
    }
}
